[FIX] Fix AbstractSortTableModel.mapFromSourceRows for empty list or array#2730
[FIX] Fix AbstractSortTableModel.mapFromSourceRows for empty list or array#2730kernc merged 1 commit intobiolab:masterfrom
Conversation
1cbeae3 to
4ccaaec
Compare
| # self.__sortInd[rows] fails if `rows` is an empty list or array | ||
| if self.__sortInd is not None \ | ||
| and (isinstance(rows, (int, type(Ellipsis))) | ||
| or len(rows)): |
There was a problem hiding this comment.
@kernc, what do you prefer:
(isinstance(rows, (int, type(Ellipsis))) or len(rows)),(not isinstance(rows, (list, np.ndarray)) or len(rows)), or(not isinstance(rows, collections.Sized) or len(rows))?
If the method is called with the argument types specified in the docstring, all are equivalent. Also, in cases where they are not equivalent (e.g. if rows is something stupid), the next line will fail anyway.
There was a problem hiding this comment.
I already see:
>>> arr = np.ones(5)
>>> empty_list = []
>>> arr[empty_list]
array([], dtype=float64)I.e. empty input, empty output.
You don't?
There was a problem hiding this comment.
I would prefer:
if self.__sortInd is not None:
if rows is not Ellipsis:
rows = np.asarray(rows, dtype=int) # dangerous casting
new_rows = self.__sortInd[rows]
...But this also relies on indexing (at least) with an empty array working.
I am not aware of this ever not being the case.
There was a problem hiding this comment.
I would prefer your first option. Note to also fix .mapFromSourceRows() below.
There was a problem hiding this comment.
dtype must not be float; the caller must ensure that. This is similar to __getitem__ which expects ints, and doesn't cast float to int. I made this clear in the docstring.
I fixed mapFromSourceRows().
Codecov Report
@@ Coverage Diff @@
## master #2730 +/- ##
==========================================
+ Coverage 76.04% 76.05% +<.01%
==========================================
Files 338 338
Lines 59643 59654 +11
==========================================
+ Hits 45358 45370 +12
+ Misses 14285 14284 -1 |
|
|
911f556 to
0f53884
Compare
|
If I understood your comments (and their removals) correctly, this is complete now. I also improved the tests for the two mappings, so they now actually test something. :) |
0f53884 to
54c5e9f
Compare
|
Yes. Even so, I dared push some improvements while the build remains untamed. |
54c5e9f to
d83130c
Compare
|
Feel free to dare. I force-pushed, hope it builds now. (Sorry for removing your name from the commit; it disappeared while resolving merge conflicts(?!).) |
6a25001 to
6d6ff33
Compare
|
AppVeyor: Anybody, help? |
6d6ff33 to
fcb2da3
Compare
|
This looks like a "segfault" to me, no idea why it happens though. It usually happens when an error occurs when destroying objects upon closing of the interpreter. |
fcb2da3 to
5854208
Compare
|
That's what one gets for squandering over sensible modifications. |
Issue
Fixes #2728.
Description of changes
Test whether a list is empty before using it for indexing an array.
Includes